from django.shortcuts import render, redirect, HttpResponse
from web.forms.project import ProjectModelForm
from django.http import JsonResponse

from web import models


def project_list(request):
    """ 项目列表显示 """
    if request.method == 'GET':
        project_dict = {'star': [], 'my': [], 'join': []}
        # 查询当前用户所有创建的项目
        my_list = models.Project.objects.filter(creator=request.user_obj)
        for row in my_list:
            if row.star:
                project_dict['star'].append({'value': row, 'type': 'my'})
            else:
                project_dict['my'].append(row)
        # 查询当前用户所有参与的项目
        join_list = models.ProjectUser.objects.filter(user=request.user_obj)
        for row in join_list:
            if row.star:
                project_dict['star'].append({'value': row, 'type': 'join'})
            else:
                project_dict['join'].append(row)

        form = ProjectModelForm(request)
        context = {
            'form': form,
            'project_dict': project_dict
        }
        return render(request, 'project_list.html', context)
    form = ProjectModelForm(request, data=request.POST)
    if form.is_valid():
        # 添加项目
        form.instance.creator = request.user_obj
        form.save()
        return JsonResponse({'status': True})
    return JsonResponse({'status': False, 'error': form.errors})


def project_star(request, project_type, project_id):
    """星标项目"""
    if project_type == 'my':
        models.Project.objects.filter(id=project_id, creator=request.user_obj).update(star=True)
        return redirect('project_list')

    if project_type == 'join':
        models.ProjectUser.objects.filter(project_id=project_id, user=request.user_obj).update(star=True)
        return redirect('project_list')
    return HttpResponse('请求错误')


def project_unstar(request, project_type, project_id):
    """去除星标"""
    if project_type == 'my':
        models.Project.objects.filter(id=project_id, creator=request.user_obj).update(star=False)
        return redirect('project_list')

    if project_type == 'join':
        models.ProjectUser.objects.filter(project_id=project_id, user=request.user_obj).update(star=False)
        return redirect('project_list')
    return HttpResponse('请求错误')
